package com.fingard.dsp.bank.directbank.fngrd01;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fingard.dsp.bank.directConfig.ActSetItem;
import com.fingard.dsp.bank.directbank.RefundState;
import com.fingard.dsp.bank.directbank.bankInterface.IRecRefund;
import com.fingard.dsp.bank.reqrespobj.recrefund.ReqQryRecRefund;
import com.fingard.dsp.bank.reqrespobj.recrefund.ReqRecRefund;
import com.fingard.dsp.bank.reqrespobj.recrefund.RetAtsQryRecRefund;
import com.fingard.dsp.bank.reqrespobj.recrefund.RetAtsRecRefund;
import com.fingard.text.StringHelper;

import java.util.HashMap;

public class FnGrd01RecRefund extends FnGrd01Base implements IRecRefund {
    @Override
    public RetAtsRecRefund submitRecRefund(ReqRecRefund p_reqRef) throws Exception {
        RetAtsRecRefund retAts = new RetAtsRecRefund();
        retAts.oriRdSeq = p_reqRef.oriRdSeq;
        retAts.refundNo = p_reqRef.refundNo;

        try {
            ActSetItem actItem = this.getBankConfig().getActSetItem(p_reqRef.ownAct, "unifiedorder");
            if (actItem != null) {
                // 下单业务参数

                /** 签名 */
                String signature = "";

                HashMap<String, String> mapParams = new HashMap<String, String>();

                mapParams.put("EnterpriseNum", actItem.inMerchantId);
                mapParams.put("MchId", actItem.merchantId);
                mapParams.put("OutTradeNo", p_reqRef.oriRdSeq);
                mapParams.put("FGTradeNo", "");
                mapParams.put("RefundNo", p_reqRef.refundNo);
                mapParams.put("TotalFee", StringHelper.amountMultiply100(p_reqRef.totalAmt));
                mapParams.put("RefundFee", StringHelper.amountMultiply100(p_reqRef.amount));
                mapParams.put("RefundChannel", "ORIGINAL");
                mapParams.put("GoodsDetail", "");
                mapParams.put("OrganizationId", "");
                mapParams.put("OperatorId", "");
                mapParams.put("TerminalId", "");
                mapParams.put("NonceStr", get_nonce_str());

                // 签名
                String signStr = getSignStr(mapParams, actItem.saltKey);
                signature = signFnGrd(actItem, signStr);

                mapParams.put("Signature", signature);

                // post请求数据
                String reqPostJson = getReqPostJson(mapParams);
                String reqSendData = getReqSendData(actItem.inMerchantId, reqPostJson);

                String[] tmpStrRet = sendToFnGrd(reqSendData, bankFront.getUrl("submitRecRefund"));

                // 解析融汇返回
                if (tmpStrRet[0].length() == 0) {
                    if (tmpStrRet[1].indexOf("Status") > 0) {
                        JSONObject respJSON = JSON.parseObject(tmpStrRet[1]);

                        String status = respJSON.getString("Status");
                        if ("-1".equals(status)) {
                            retAts.respCode = "-1";
                            retAts.respInfo = respJSON.getString("ReturnMsg");
                            return retAts;
                        }
                    } else {
                        String respStr = getRespPostData(tmpStrRet[1]);
                        JSONObject respJSONObj = JSON.parseObject(respStr);

                        retAts.respCode = "0";
                        retAts.respInfo = respJSONObj.getString("ReturnMsg");
                        String status = respJSONObj.getString("Status");
                        if ("0".equalsIgnoreCase(status)) {
                            retAts.refundState = RefundState.REFUND_PAYING;
                            retAts.originalCode = status;
                            retAts.originalInfo = respJSONObj.getString("ReturnMsg");
                            retAts.unifiedCode = "I4001";
                        } else {
                            retAts.respCode = "-1";
                        }
                    }
                } else {
                    retAts.respCode = "-1";
                    retAts.respInfo = tmpStrRet[1];
                }
            } else {
                retAts.respCode = "0";
                retAts.respInfo = "未找到账号[" + p_reqRef.ownAct + "]商户配置信息!";
                retAts.refundState = RefundState.REFUND_FAIL;
                retAts.originalCode = "E9981";
                retAts.originalInfo = retAts.respInfo;
                retAts.unifiedCode = "E9981";
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            retAts.respCode = "-1";
            retAts.respInfo = ex.getMessage();
            WriteBankLogLn(ex);
        }

        return retAts;
    }

    @Override
    public RetAtsQryRecRefund queryRecRefund(ReqQryRecRefund p_reqQryRef) throws Exception {
        RetAtsQryRecRefund retAts = new RetAtsQryRecRefund();
        retAts.oriRdSeq = p_reqQryRef.oriRdSeq;
        retAts.refundNo = p_reqQryRef.refundNo;

        try {
            ActSetItem actItem = this.getBankConfig().getActSetItem(p_reqQryRef.ownAct, "unifiedorder");
            if (actItem != null) {
                String signature = "";

                HashMap<String, String> mapParams = new HashMap<String, String>();

                mapParams.put("EnterpriseNum", actItem.inMerchantId);
                mapParams.put("MchId", actItem.merchantId);
                mapParams.put("OutTradeNo", p_reqQryRef.oriRdSeq);
                mapParams.put("RefundNo", p_reqQryRef.refundNo);
                mapParams.put("FGRefundNo", "");
                mapParams.put("NonceStr", get_nonce_str());

                // 签名
                String signStr = getSignStr(mapParams, actItem.saltKey);
                signature = signFnGrd(actItem, signStr);

                mapParams.put("Signature", signature);

                // post请求数据
                String reqPostJson = getReqPostJson(mapParams);
                String reqSendData = getReqSendData(actItem.inMerchantId, reqPostJson);

                String[] tmpStrRet = sendToFnGrd(reqSendData, bankFront.getUrl("queryRecRefund"));

                // 解析融汇返回
                if (tmpStrRet[0].length() == 0) {
                    if (tmpStrRet[1].indexOf("Status") > 0) {
                        JSONObject respJSON = JSON.parseObject(tmpStrRet[1]);

                        String status = respJSON.getString("Status");
                        if ("-1".equals(status)) {
                            retAts.respCode = "-1";
                            retAts.respInfo = respJSON.getString("ReturnMsg");
                            return retAts;
                        }
                    } else {
                        String respStr = getRespPostData(tmpStrRet[1]);
                        JSONObject respJSONObj = JSON.parseObject(respStr);

                        String status = respJSONObj.getString("Status");
                        if ("0".equals(status)) {
                            retAts.respCode = "0";
                            retAts.respInfo = respJSONObj.getString("ReturnMsg");

                            String refundState = respJSONObj.getString("RefundStatus");
                            if ("SUCCESS".equals(refundState)) {
                                retAts.refundState = RefundState.REFUND_SUCCESS;
                            } else if ("FAIL".equals(refundState) || "CHANGE".equals(refundState)) {
                                retAts.refundState = RefundState.REFUND_FAIL;
                            } else  {
                                retAts.refundState = RefundState.REFUND_PAYING;
                            }
                            retAts.originalCode = refundState;
                            retAts.originalInfo = respJSONObj.getString("ReturnMsg");
                            retAts.unifiedCode = getBankConfig().getMapValueByKey("recRefundMapping", refundState);
                        } else {
                            String returnMsg = respJSONObj.getString("ReturnMsg");
                            retAts.respCode = "-1";
                            retAts.respInfo = status + "+" + returnMsg;
                            if ("该商户不存在此退款记录".equalsIgnoreCase(returnMsg)) {
                                retAts.respCode = "0";
                                retAts.respInfo = returnMsg;
                                retAts.refundState = RefundState.REFUND_FAIL;
                                retAts.originalCode = status;
                                retAts.originalInfo = returnMsg;
                                retAts.unifiedCode = "E1302";
                            }
                        }
                    }
                } else {
                    retAts.respCode = "-1";
                    retAts.respInfo = tmpStrRet[1];
                }
            } else {
                retAts.respCode = "-1";
                retAts.respInfo = "未找到账号[" + p_reqQryRef.ownAct + "]商户配置信息!";
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            retAts.respCode = "-1";
            retAts.respInfo = ex.getMessage();
            WriteBankLogLn(ex);
        }

        return retAts;
    }
}